/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mahumLogs.ejb;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.Session;
import com.mahumLogs.backedbeans.Configuration;
import com.mahumLogs.beans.ConfigurationBean;
import com.mahumLogs.utils.MyReader;
import com.mahumLogs.utils.SSH;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import javax.ejb.Schedule;
import javax.ejb.Singleton;
import javax.inject.Inject;

/**
 *
 * @author Kalast
 */
@Singleton
public class PeriodicScanEJB implements Serializable {

    private static long lastReadLineLog = 0;
    private static HashMap<String, String> config = new HashMap();
    private static boolean execution = false;
    private String status;

    @Schedule(second = "*/5", minute = "*", hour = "*", persistent = false)
    public void doWork() {
        /*System.out.println("Tache toutes les 5 secondes");
        if (execution) {
            System.out.println("On récupère tous les logs depuis la dernière execution");
            String squidAddress = config.get("SQUID_SERVER_IP");
            String squidUser = config.get("SQUID_SERVER_USER");
            String squidPwd = config.get("SQUID_SERVER_PWD");
            String logsPath = config.get("SQUID_PATH_LOGS");
            
            System.out.println(squidAddress+ "@" + squidUser + " -f=" + logsPath + " -l=" + lastReadLineLog);
            try {
                Session s = SSH.createSession(squidAddress, squidUser, squidPwd);
                status = "Tentative de reconnexion à " + squidAddress+ "@" + squidUser;
                Channel c = SSH.exec(s, "find " + logsPath);
                MyReader my_reader = new MyReader(new InputStreamReader(c.getInputStream()));
                String line = my_reader.readLine();
                if (line != null) {
                    System.out.println(line);
                    c.disconnect();
                    s.disconnect();
                    status = "Connexion OK";
                }
            } catch (Exception ex) {
                System.out.println("On a perdu la connexion ...");
                status = "Connexion à " + squidAddress+ "@" + squidUser + " perdue...";
            }
        } else {
            status = "En attente d'un scan";
            System.out.println("Un scan est necessaire");
        }
*/
    }

    public String getStatus() {
        return status;
    }

    public PeriodicScanEJB() {
    }

    public static void majInfos(List<Configuration> configs) {
        for (Configuration c : configs) {
            PeriodicScanEJB.config.put(c.getCle(), c.getValeur());
        }
    }

    public static void startScan(String squidAddress, String squidUser, String squidPwd, String logsPath, long lastLineRead) {
        PeriodicScanEJB.lastReadLineLog = lastLineRead;
        PeriodicScanEJB.execution = true;
    }
}
